<?php

namespace app\beautiful\controller\miniapp\member\project;

use app\BaseController;

use app\beautiful\logic\order\OrderLogic;

use app\beautiful\logic\PartnerLogic;
use app\beautiful\model\project\OnceCardItemModel;

use app\beautiful\model\project\OnceCardModel;

use app\beautiful\model\member\MemberOnceCardModel;

use app\beautiful\model\logs\ProjectWriteOffModel;

use think\facade\Db;



class Oncecard extends BaseController{

    

    //获取我的次卡列表

    public function getMemberOncecardList(){

        $type = $this->request->param('type/d');

        $where = [

            ['shop_id','=',$this->request->shopId],

            ['member_id','=', $this->request->memberId]

        ];

        $today = date('Y-m-d',time());

        

        if($type == 0){

            $where[] = ['status','=',1];

            $where[] = ['valid_end_time','>=',$today];

            $query = MemberOnceCardModel::where($where);

        }else{

     

            $where1 = $where;

            $where2 = $where;

            $where1[] = ['status','=',8];

            $where2[] =  ['valid_end_time','<',$today];

            

            $query = MemberOnceCardModel::whereOr([$where1,$where2]);

        }

        $datas = $query->order(['member_once_card_id'=>'desc'])->page($this->request->page)->limit($this->request->limit)->select()->append(['status_another'])->toArray();

        return $this->success(['datas'=>$datas]);

    }

    

    public function getMemberOncecardDetail(){

        $member_once_card_id = $this->request->param('member_once_card_id/d');

        if(empty($member_once_card_id)){

            return $this->error('请选择要查看的次卡');

        }

        if(null === $card = MemberOnceCardModel::find($member_once_card_id)){

            return $this->error('请选择要查看的次卡');

        }

        if($card->member_id != $this->request->memberId || $card->shop_id != $this->request->shopId){

            return $this->error('请选择要查看的次卡');

        }

        $card->items;

        $card->append(['status_means','status_another','pay_time_format','pay_type_mean'])->toArray();

        return $this->success([

            'detail' => $card

        ]);

    }

    public function getMemberOncecardLogs(){

        $member_once_card_id = $this->request->param('member_once_card_id/d');

        if(empty($member_once_card_id)){

            return $this->error('请选择要查看的次卡');

        }

        if(null === $card = MemberOnceCardModel::find($member_once_card_id)){

            return $this->error('请选择要查看的次卡');

        }

        if($card->member_id != $this->request->memberId || $card->shop_id != $this->request->shopId){

            return $this->error('请选择要查看的次卡');

        }

        $where = [

            ['shop_id','=', $this->request->shopId],

            ['member_id','=', $this->request->memberId],

            ['source','=','once_card'],

            ['member_once_card_id','=',$member_once_card_id]

        ];

        $datas = ProjectWriteOffModel::where($where)->select()->append(['add_time_format'])->toArray();

        return $this->success([

            'datas' => $datas

        ]);

    }

    

    

    public function getOncecardList()

    {

        $where=[

            ['shop_id','=',$this->request->shopId],

            ['is_delete','=',0],

            ['status','=',1],

        ];



        $once_card_model = new OnceCardModel();

        $order=['sort'=>'desc','add_time'=>'desc'];

        $field='once_card_id,name,cover_img,price,worth,is_flash_sale,flash_price,flash_num,start_time,end_time,fictitious_take_count';

        $append=['is_flash_sale_expire','flash_sale_djs','save_money'];

        $datas=$once_card_model->where($where)->order($order)

            ->field($field)

            ->limit($this->request->limit)

            ->page($this->request->page)

            ->select()

            ->append($append)

            ->toArray();

        $return['datas']=$datas;

        return $this->success($return);



    }





    public function getOncecardDetail()

    {

        $once_card_id=$this->request->param('once_card_id/d',0);

        if($once_card_id==0) return $this->error('次卡不存在');



        if(null === $once_card = OnceCardModel::find($once_card_id)) return $this->error('次卡不存在');

        if($once_card->shop_id != $this->request->shopId) return $this->error('次卡不存在');

        if($once_card->is_delete != 0) return $this->error('次卡不存在');



        $items=OnceCardItemModel::with(['project'=>function($query){

            $query->field('project_id,cover_img,abbreviation,market_price');

        }])->where([

            ['shop_id','=',$this->request->shopId],

            ['once_card_id','=',$once_card->once_card_id],

            ['is_delete','=',0]

        ])->field('project_id,num')->select()->toArray();





        $detail=[

            'name'=>$once_card->name,

            'price'=>$once_card->price,

            'is_flash_sale'=>$once_card->is_flash_sale,

            'flash_price'=>$once_card->flash_price,

            'is_flash_sale_expire'=>$once_card->is_flash_sale_expire,

            'flash_sale_djs'=>$once_card->flash_sale_djs,

            'save_money'=>$once_card->save_money,

            'valid_time'=>$once_card->valid_time,

            'valid_unit'=>$once_card->valid_unit,

            'fictitious_take_count'=>$once_card->fictitious_take_count,

            'cover_img'=>$once_card->cover_img,

            'rotation'=>$once_card->rotation,

            'items'=>$items,

            'use_info'=>$once_card->use_info,

        ];



        return $this->success(['detail'=>$detail]);

    }



    public function createOrder()

    {

        $once_card_id=$this->request->param('once_card_id/d',0);

        $total_price = $this->request->param('total_price/f',0);

        $use_integral = $this->request->param('use_integral/d',0);

        $coupon_id = $this->request->param('coupon_id/d',0);

        $from_type = $this->request->param('from_type/d',1);

        $params=[

            'member_id'=>$this->request->memberId,

            'total_price'=>$total_price,

            'use_integral'=>$use_integral,

            'coupon_id'=>$coupon_id,

            'once_card_id'=>$once_card_id,

            'from_type'=>$from_type

        ];

        Db::startTrans();

        try{

            $order_logic = OrderLogic::init($this->request->shopId);

            $order_id=$order_logic['once_card']->createOrder($params);

            Db::commit();

            return $this->success(['order_id'=>$order_id]);

        } catch (\Exception $e){

            Db::rollback();

            return $this->error($e->getMessage());

        }

    }



    public function moneyPay()

    {

        $once_card_order_id=$this->request->param('once_card_order_id/d',0);

        $params=[

            'member_id'=>$this->request->memberId,

            'once_card_order_id'=>$once_card_order_id,

        ];

        Db::startTrans();

        try{

            $order_logic = OrderLogic::init($this->request->shopId);

            $need_pay = $order_logic['once_card']->moneyPay($params);

            //增加分红
            $PartnerLogic = new PartnerLogic($this->request->shopId);
            $PartnerLogic->commissionNew($this->request->memberId,$need_pay,'onceCardOrder',$once_card_order_id);


            Db::commit();

            return $this->success('余额支付成功');

        } catch (\Exception $e){

            Db::rollback();

            return $this->error($e->getMessage());

        }

    }



    public function cancelOrder()

    {

        $once_card_order_id=$this->request->param('once_card_order_id/d',0);

        $params=[

            'member_id'=>$this->request->memberId,

            'once_card_order_id'=>$once_card_order_id,

        ];

        Db::startTrans();

        try{

            $order_logic = OrderLogic::init($this->request->shopId);

            $order_logic['once_card']->cancelOrder($params);

            Db::commit();

            return $this->success('取消订单成功');

        } catch (\Exception $e){

            Db::rollback();

            return $this->error($e->getMessage());

        }

    }

}

